/**
 * 
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import model.KhoiKienThucM;
import model.LoaiNhomMonHocM;
import model.NhomMonHocM;
import util.DAL;
import util.MyConnectUnit;

public class NhomMonHocD {
	private MyConnectUnit conn;
	/**
	 * @throws Exception 
	 * @function constructor NhomMonHocD
	 */
	public NhomMonHocD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}
	
	/**
	 * @function get all NhomMonHoc
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonHocM> getAlls(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<NhomMonHocM> ls = new ArrayList<NhomMonHocM>();
		ResultSet rs = this.conn.Selects(
						"id_nhom_mon_hoc, nmh.ten, nmh.id_loai_nhom_mon_hoc, lnmh.ten, nmh.id_khoi_kien_thuc, kkt.ten, so_tc, ghi_chu",
						"NHOM_MON_HOC nmh, LOAI_NHOM_MON_HOC lnmh, KHOI_KIEN_THUC kkt",
						"nmh.id_loai_nhom_mon_hoc = lnmh.id_loai_nhom_mon_hoc AND nmh.id_khoi_kien_thuc = kkt.id_khoi_kien_thuc AND nmh.ten LIKE '%" + ten + "%'",
						"id_nhom_mon_hoc ASC",
						limit);
		NhomMonHocM l = null;
		while (rs.next()) {
			l = new NhomMonHocM();
			l.setId_nhom_mon_hoc(rs.getInt("id_nhom_mon_hoc"));
			l.setTen(rs.getString("nmh.ten"));
			
			l.setId_loai_nhom_mon_hoc(rs.getInt("nmh.id_loai_nhom_mon_hoc"));
			LoaiNhomMonHocM lnmh = new LoaiNhomMonHocM();
			lnmh.setTen(rs.getString("lnmh.ten"));
			l.setTen_loai_nhom_mon_hoc(lnmh);
			
			l.setId_khoi_kien_thuc(rs.getInt("nmh.id_khoi_kien_thuc"));
			KhoiKienThucM kkt = new KhoiKienThucM();
			kkt.setTen(rs.getString("kkt.ten"));
			l.setTen_khoi_kien_thuc(kkt);
			
			l.setSo_tc(rs.getInt("so_tc"));
			l.setGhi_chu(rs.getString("ghi_chu"));
			ls.add(l);
		}
		return ls;
	}
	
	/**
	 * @function get all NhomMonHoc
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonHocM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<NhomMonHocM> ls = new ArrayList<NhomMonHocM>();
		ResultSet rs = this.conn.Select("NHOM_MON_HOC", Condition, Limit, OrderBy);
		NhomMonHocM l = null;
		while (rs.next()) {
			l = new NhomMonHocM();
			l.setId_nhom_mon_hoc(rs.getInt("id_nhom_mon_hoc"));
			l.setTen(rs.getString("ten"));		
			l.setId_loai_nhom_mon_hoc(rs.getInt("id_loai_nhom_mon_hoc"));	
			l.setId_khoi_kien_thuc(rs.getInt("id_khoi_kien_thuc"));
			l.setSo_tc(rs.getInt("so_tc"));
			l.setGhi_chu(rs.getString("ghi_chu"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all NhomMonHocM with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonHocM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all NhomMonHocM with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonHocM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all NhomMonHocM no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonHocM> getAll() throws Exception {
		return getAll(null);
	}
	
	/**
	 * @function search ten NhomMonHocM
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonHocM> searchNMH(String ten) throws Exception {
		ArrayList<NhomMonHocM> list = getAll("ten LIKE '%" + ten + "%'");
		return list;
	}
	
	/**
	 * @function get all NhomMonHocM with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonHocM> getLimit(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<NhomMonHocM> list = getAll("ten LIKE '%" + ten + "%'", limit);
		return list;
	}

	/**
	 * function insert NhomMonHocM
	 * @param nmh
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(NhomMonHocM nmh) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", nmh.getTen());
		map.put("id_loai_nhom_mon_hoc", nmh.getId_loai_nhom_mon_hoc());
		map.put("id_khoi_kien_thuc", nmh.getId_khoi_kien_thuc());
		map.put("so_tc", nmh.getSo_tc());
		map.put("ghi_chu", nmh.getGhi_chu());
		return this.conn.Insert("NHOM_MON_HOC", map);
	}

	/**
	 * @function update NhomMonHoc
	 * @param nmh
	 * @return
	 * @throws Exception
	 */
	public boolean Update(NhomMonHocM nmh) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", nmh.getTen());
		map.put("id_loai_nhom_mon_hoc", nmh.getId_loai_nhom_mon_hoc());
		map.put("id_khoi_kien_thuc", nmh.getId_khoi_kien_thuc());
		map.put("so_tc", nmh.getSo_tc());
		map.put("ghi_chu", nmh.getGhi_chu());
		return this.conn.Update("NHOM_MON_HOC", map, "id_nhom_mon_hoc = " + nmh.getId_nhom_mon_hoc());
	}

	/**
	 * @function delete NhomMonHoc
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("NHOM_MON_HOC", "id_nhom_mon_hoc = " + id);
	}
}